<template>
  <div class="zhuj">
    <van-submit-bar :price="prices" :button-text="text" @submit="onSubmit">
      <van-checkbox
        v-model="checked"
        @click="quanxhuan"
        v-if="this.$store.state.submit"
        checked-color="#1989fa"
        >全选</van-checkbox
      >
      <van-checkbox v-model="checked" disabled v-else>全选</van-checkbox>
    </van-submit-bar>
  </div>
</template>

<script>
import { Toast } from "vant";
import API from "../../http/API";
import eventVue from "../../assets/zizhujian/index";
import { Checkbox, CheckboxGroup } from "vant";
import { SubmitBar } from "vant";
export default {
  name: "tijiao",
  components: {
    [SubmitBar.name]: SubmitBar,
    [Checkbox.name]: Checkbox,
    [CheckboxGroup.name]: CheckboxGroup,
  },
  props: ["switch"],
  // 注入方法让购物车页面重新拿到数据
  inject: ["reload"],
  data() {
    return {
      text: "结算",
      checked: false,
      prices: 0,
      // 如果没有全选就把当前选项的传过去把所有数据拿到
      commodity: [],
      // check all 拿来判断是不是全选的
      checkall: false,
      state: 0,
      // 全选事件
      isquanx: 0,
    };
  },
  watch: {
    switch(e) {
      console.log(e);
      if (e == 1) {
        this.state = e;
        this.text = "删除";
      } else {
        this.state = e;
        this.text = "结算";
      }
    },
  },
  created() {
    this.select();
    this.xuanzhe();
  },
  deactivated(){
    this.checked=false
    this.prices=0
  },
  methods: {
    
gwc() {
  this.$get(API.GET_CART_INDEX, {
    token: this.$store.state.token,
  }).then((res) => {
    if (res.errmsg == "成功") {
    
        this.$store.commit("shoppingAmount", res.data.cartList.length);
     
      
    
    }
  });
},

    // 兄弟组件传过来数据如果是全选的就设置为true
    xuanzhe: function () {
      eventVue.$on("quanx", (res) => {
        // console.log(res);

        this.checked = res;
      });
    },
    onSubmit(e) {
      switch (this.state) {
        case 0:
          this.$router.push("/confirm");
          break;
        case 1:
          const toast = Toast.loading({
            duration: 0, // 持续展示 toast
            forbidClick: true,
            message: "正在删除",
          });
          this.deletes();
          break;
      }
    },
    // 删除购物车功能
    deletes() {
      // 删除
      console.log(this.commodity);
      // 用来保存要删除的商品id

      let arr = [];
      for (let index = 0; index < this.commodity.length; index++) {
        if (this.commodity[index].checked == true) {
          arr.push(this.commodity[index].productId);
        }
      }

      this.$post(API.POST_CART_DELETE, {
        token: this.$store.state.token,
        productIds: arr,
      })
        .then((res) => {
          if (res.errmsg == "成功") {
            Toast.clear();
            Toast("删除成功");
        this.gwc()
            this.select();
            this.reload()
            this.prices = 0;
          }
        })
        .catch((err) => {});
    },
    onClickEditAddress() {},

    select: function () {
      eventVue.$on("sum", (res) => {
        //这里最好用箭头函数，不然this指向有问题
        // this.btext = message;
        // console.log(res);
        this.prices = res.cartTotal.checkedGoodsAmount * 100;
        this.commodity = res.cartList;
        // console.log(res.checkedGoodsAmount);
        // console.log(res);
      });
    },
    quanxhuan(e) {
      // 如果是不是全选传到orderlist页面去
      // 发送全选事件过去
      this.checkall = this.checked;
      eventVue.$emit("checkbox", this.checked);
    },
  },
};
</script>

<style lang="less" scoped>
.zhuj {
  position: fixed;
  /* height: 100%; */
  width: 100%;
  .van-submit-bar {
    position: relative;
  }
  .jiazai {
    width: 100%;
    border: 1px solid #000;
    position: fixed;
    z-index: 99;
    top: 50%;
    // right: 0;
  }
}
</style>
